package com.byit.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.builder.MapBuilder;
import com.byit.constant.DefaultValue;
import com.byit.domain.SysUser;
import com.byit.domain.SysUserRole;
import com.byit.enums.PromptInfo;
import com.byit.log.ChannelType;
import com.byit.log.Log;
import com.byit.log.OperatorType;
import com.byit.msg.MessageResult;
import com.byit.service.IAppUserService;
import com.byit.service.ISysUserRoleService;
import com.byit.service.ISysUserService;
import com.byit.shrio.session.SessionHelper;
import com.byit.utils.PasswordUtil;
import com.byit.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author wangx
 * @since 2018-12-11
 */
@RestController
@RequestMapping("/api/sys/appUser")
public class AppUserController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IAppUserService appUserService;


    /**
     * 按条件查询所有用户信息
     *
     * @param session
     * @param phone
     * @param userName
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    @Log(title = "用户查询", operate = OperatorType.DATA_SELECT, channel = ChannelType.WEB_INTER)
    public Object selectUserList(HttpSession session,
                                 @RequestParam(value = "phone", defaultValue = "") String phone,
                                 @RequestParam(value = "idCard", defaultValue = "") String idCard,
                                 @RequestParam(value = "userName", defaultValue = "") String userName,
                                 @RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
                                 @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit) {

        Page<SysUser> pageInfo = new Page<>(page, limit);
        Map<String, Object> params = MapBuilder.newMap(3).ok();
        if (null != userName && !"".equals(userName)) {
            params.put("userName", "%" + StringUtil.decodeSpecialCharsWhenLikeUseBackslash(userName) + "%");
        }
        if (null != phone && !"".equals(phone)) {
            params.put("phone", "%" + StringUtil.decodeSpecialCharsWhenLikeUseBackslash(phone) + "%");
        }
        if (null != idCard && !"".equals(idCard)) {
            params.put("idCard", "%" + StringUtil.decodeSpecialCharsWhenLikeUseBackslash(idCard) + "%");
        }
        Page<JSONObject> result = appUserService.selectUserList(pageInfo, params);
        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, result);
    }


}
